import JDBC.JDBCUtils;
import Medicine.Pharmacy;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import user.Buyman;
import user.Man;
import user.Sellsman;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class Main3 {
    //店员
    public static String loginmenu1() throws SQLException {
        Scanner sc = new Scanner(System.in);

        String name = "";

        int flag = 0;
        while (flag==0) {
            System.out.println("*********药店管理系统操作面板***********");
            System.out.println("*******欢迎进入药店管理系统,请选择*******");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("0.退出");
            System.out.println("************************************");
            int input = sc.nextInt();

            switch (input) {
                case 0:
                    System.exit(0);
                    break;
                case 1:
                    name = canlogin1();

                    return name;

                case 2:
                    register1();
                    break;
                default:
                    System.out.println("请重新输入");
                    break;
            }
        }
        return "abc";
    }
    //店员
    public static String canlogin1()  {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        Scanner sc = new Scanner(System.in);
        String user = "";
        String password = "";

        String name = "";

        while (true) {
            System.out.println("请输入您的账号：");
            user = sc.nextLine();
            System.out.println("请输入您的密码：");
            password = sc.nextLine();

            //看账号 比较密码
            String query = "SELECT password,name FROM sellsman WHERE user = ?";

            try {
                List<Map<String, Object>> sellsmanList = qr.query(query, new MapListHandler(), user);
                if (!sellsmanList.isEmpty()) {
                    Map<String, Object> sellsmanMap = sellsmanList.get(0);
                    //System.out.println("查询结果: " + sellsmanMap);
                    Object dbPassword = sellsmanMap.get("password");
                    Object dbaName = sellsmanMap.get("name");

                    if (dbPassword != null && dbPassword.equals(password)) {
                        System.out.println("登录成功！");
                        if (dbaName != null) {
                            return dbaName.toString();
                        } else {
                            System.out.println("账号信息不完整，请联系管理员。");
                            return "anonymous"; // 或者返回一个默认值
                        }
                        // 登录成功，跳出循环
                    } else {
                        System.out.println("密码错误，请重试");
                    }
                } else {
                    System.out.println("账号不存在，请重试");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库查询出错，请检查输入或联系管理员");
            }
        }

    }
    //店员
    public static void register1() throws SQLException {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        Scanner sc = new Scanner(System.in);
        String account = "";
        String password = "";
        String name = "";
        String sex = "";
        int age = 0;
        while (true) {
            System.out.println("请输入您想创建的账号:   （账号唯一）");
            account = sc.nextLine();
            String query = "SELECT user FROM sellsman";
            int flag = 0;
            List<Map<String, Object>> sellsmanlist = qr.query(query, new MapListHandler());
            for (Map<String, Object> sellsmanMap : sellsmanlist) {
                if (sellsmanMap.get("user").equals(account)) {
                    System.out.println("此账号已被使用，请重试");
                    flag = 1;
                    break;
                }
            }
            if (flag == 1) {
                continue;
            }
            System.out.println("请输入您的密码:");
            password = sc.nextLine();
            System.out.println("请再次输入您的密码:");
            String password1 = sc.nextLine();
            if (!password.equals(password1)) {
                System.out.println("两次密码不一致，请重试!");
                continue;
            }
            System.out.println("请输入您的姓名");
            name = sc.nextLine();
            System.out.println("请输入您的性别");
            sex = sc.nextLine();
            System.out.println("请输入您的年龄");
            age = sc.nextInt();
            break;
        }
        //添加进数据库
        String sql = "INSERT INTO sellsman (User,Password,Name,Sex,Age)" +
                " VALUES(?,?,?,?,?)";
        //设置传入SQL中的参数值
        Object[] parms = {account, password, name, sex, age};
        //执行插入里程碑信息的SQL
        int count = qr.update(sql, parms);
        if (count < 1) {
            System.out.println("注册失败！");
        } else {
            System.out.println("注册成功！");
            return;
        }
        sc.close();
        return;
    }
    //******************************************************************************
    //顾客
    public static String loginmenu2() throws SQLException {
        Scanner sc = new Scanner(System.in);
        String name = "";
        int flag = 0;
        while (flag==0) {
            System.out.println("*********药店管理系统买家面板***********");
            System.out.println("*******欢迎进入药店购买系统,请选择*******");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("0.退出");
            System.out.println("************************************");
            int input = sc.nextInt();

            switch (input) {
                case 0:
                    //exit();
                    System.exit(0);
                    flag=1;
                    break;
                case 1:
                    name=canlogin2();
                    return name;
                case 2:
                    register2();
                    break;
                default:
                    System.out.println("请重新输入");
                    break;
            }

        }
        return "abc";
    }
    //顾客
    public static String canlogin2()  {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        Scanner sc = new Scanner(System.in);
        String user = "";
        String password = "";

        String name = "";

        while (true) {
            System.out.println("请输入您的账号：");
            user = sc.nextLine();
            System.out.println("请输入您的密码：");
            password = sc.nextLine();

            //看账号 比较密码
            String query = "SELECT password,name FROM buyman WHERE user = ?";

            try {
                List<Map<String, Object>> sellsmanList = qr.query(query, new MapListHandler(), user);

                if (!sellsmanList.isEmpty()) {
                    Map<String, Object> sellsmanMap = sellsmanList.get(0);

                    //System.out.println("查询结果: " + sellsmanMap);

                    Object dbPassword = sellsmanMap.get("password");
                    Object dbaName = sellsmanMap.get("name");

                    if (dbPassword != null && dbPassword.equals(password)) {



                        if (dbaName != null) {
                            System.out.println("登录成功！");
                            return dbaName.toString();
                        } else {
                            System.out.println("账号信息不完整，请联系管理员。");
                            return "anonymous"; // 或者返回一个默认值
                        }
                        // 登录成功，跳出循环
                    } else {
                        System.out.println("密码错误，请重试");
                    }
                } else {
                    System.out.println("账号不存在，请重试");
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("数据库查询出错，请检查输入或联系管理员");
            }
        }

    }
    //顾客
    public static void register2() throws SQLException {
        //创建QueryRunner对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        Scanner sc = new Scanner(System.in);

        String account = "";
        String password = "";
        String name = "";
        String sex = "";
        int age = 0;
        while (true) {
            System.out.println("请输入您想创建的账号:   （账号唯一）");
            account = sc.nextLine();

            String query = "SELECT user FROM buyman";
            int flag = 0;
            List<Map<String, Object>> sellsmanlist = qr.query(query, new MapListHandler());
            for (Map<String, Object> sellsmanMap : sellsmanlist) {
                if (sellsmanMap.get("user").equals(account)) {
                    System.out.println("此账号已被使用，请重试");
                    flag = 1;
                    break;
                }
            }
            if (flag == 1) {
                continue;
            }

            System.out.println("请输入您的密码:");
            password = sc.nextLine();
            System.out.println("请再次输入您的密码:");
            String password1 = sc.nextLine();
            if (!password.equals(password1)) {
                System.out.println("两次密码不一致，请重试!");
                continue;
            }
            System.out.println("请输入您的姓名");
            name = sc.nextLine();
            System.out.println("请输入您的性别");
            sex = sc.nextLine();
            System.out.println("请输入您的年龄");
            age = sc.nextInt();
            break;
        }
        int point = 0;

        //添加进数据库
        String sql = "INSERT INTO buyman (User,Password,Name,Sex,Age,Point)" +
                " VALUES(?,?,?,?,?,?)";
        //设置传入SQL中的参数值
        Object[] parms = {account, password, name, sex, age,point};
        //执行插入里程碑信息的SQL
        int count = qr.update(sql, parms);
        if (count < 1) {
            System.out.println("注册失败！");

        } else {
            System.out.println("注册成功！");
            return;
        }
        sc.close();
        return;

    }


    public static Man login() throws SQLException {
        Scanner sc = new Scanner(System.in);

        String name = "";

        while (true) {
            System.out.println("请确认您的身份信息：1:购买者 2:药店店员");
            int input = sc.nextInt();
            switch (input) {
                case 0:
                    //离开
                    System.exit(0);
                    break;
                case 1:
                    System.out.println("欢迎光临！！！");
                    name = loginmenu2();
                    return new Buyman(name);

                case 2:
                    System.out.println("欢迎药店店员！！！");
                    name = loginmenu1();
                    return new Sellsman(name);
                default:
                    System.out.println("请重新输入");
                    break;
            }
        }

//        int choice = sc.nextInt();
//        while (true) {
//            if (choice == 1) {
//                return new Buyman(name);
//            } else if (choice == 2) {
//                return new Sellsman(name);
//            } else {
//                System.out.println("输入信息无法识别，请重新输入");
//                System.out.println("请确认您的身份信息：1：药店店员 2.购买者");
//                choice = sc.nextInt();
//            }
//        }
    }

    public static void main(String[] args) throws SQLException {
        //需要 Pharmacy类 中的Medicine数组
        Pharmacy pharmacy = new Pharmacy();

        Man man = login();
        //DataBaseConnection.MysqlConnect1();
        while (true) {
            int choice = man.menu();
            man.doOperations(choice, pharmacy);
            //退出系统时结束循环
        }
    }
}

